<template>
    <div class="related-articles">
        <div class="related-title">
            <i class="fas fa-link"></i>
            相关文章推荐
        </div>

        <!-- Skeleton Loader -->
        <div v-if="loading" class="related-list-skeleton">
            <div v-for="n in 3" :key="n" class="skeleton-item">
                <div class="skeleton-cover"></div>
                <div class="skeleton-info">
                    <div class="skeleton-title"></div>
                    <div class="skeleton-meta"></div>
                </div>
            </div>
        </div>

        <!-- Actual Content -->
        <div v-else>
            <transition-group name="fade-list" tag="div" class="related-list" v-if="relatedArticles.length > 0">
                <div v-for="article in relatedArticles" :key="article.id" class="related-item"
                    @click="handleClick(article)">
                    <div class="related-item-cover">
                        <img v-lazy="article.cover" :alt="article.title" class="cover-image" />
                        <div class="cover-overlay">
                            <i class="fas fa-link"></i>
                        </div>
                    </div>
                    <div class="related-item-info">
                        <h4 class="related-item-title">{{ article.title }}</h4>
                        <div class="related-item-meta">
                            <span v-if="article.categoryName" class="category-tag">
                                <i class="fas fa-folder"></i> {{ article.categoryName }}
                            </span>
                            <span v-if="article.quantity >= 0" class="read-count">
                                <i class="fas fa-eye"></i> {{ article.quantity }} 阅读
                            </span>
                        </div>
                    </div>
                </div>
            </transition-group>
            <div class="empty-related" v-else>
                <i class="fas fa-search"></i>
                <p>暂无相关文章</p>
            </div>
        </div>
    </div>
</template>

<script>
import { getRelatedArticlesApi } from '@/api/article';

export default {
    name: 'RelatedArticles',

    props: {
        articleId: {
            type: [Number, String],
            required: true
        },
        limit: {
            type: Number,
            default: 5
        }
    },

    data() {
        return {
            relatedArticles: [],
            loading: false
        };
    },

    watch: {
        articleId: {
            handler(newId) {
                if (newId) {
                    this.fetchRelatedArticles();
                }
            },
            immediate: true
        }
    },

    methods: {
        async fetchRelatedArticles() {
            this.loading = true;

            try {
                const res = await getRelatedArticlesApi(this.articleId, this.limit);
                if (res.data && res.data.length > 0) {
                    // 添加短暂延迟，让骨架屏有更好的视觉效果
                    setTimeout(() => {
                        this.relatedArticles = res.data;
                        this.loading = false;
                    }, 300);
                } else {
                    this.relatedArticles = [];
                    this.loading = false;
                }
            } catch (error) {
                console.error('获取相关文章失败:', error);
                this.relatedArticles = [];
                this.loading = false;
            }
        },

        handleClick(article) {
            if (article.id) {
                // 如果是当前文章，不进行跳转
                if (article.id == this.articleId) {
                    return;
                }
                this.$router.push(`/post/${article.id}`);
            }
        }
    }
};
</script>

<style lang="scss" scoped>
@keyframes skeleton-shine {
    0% {
        background-position: -200px 0;
    }

    100% {
        background-position: calc(200px + 100%) 0;
    }
}

// 列表过渡动画
.fade-list-enter-active,
.fade-list-leave-active {
    transition: all 0.5s;
}

.fade-list-enter-from {
    opacity: 0;
    transform: translateY(20px);
}

.fade-list-leave-to {
    opacity: 0;
    transform: translateY(-20px);
}

.fade-list-move {
    transition: transform 0.5s;
}

.related-list-skeleton {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 20px;

    .skeleton-item {
        display: flex;
        background: var(--card-bg, #f9f9f9);
        border-radius: 12px;
        overflow: hidden;
        padding: 10px;
        border: 1px solid var(--border-color, #eaeaea);
        transition: all 0.3s ease;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);

        .skeleton-cover,
        .skeleton-title,
        .skeleton-meta {
            background-color: #e0e0e0;
            background-image: linear-gradient(90deg,
                    #e0e0e0 0px,
                    #f5f5f5 40px,
                    #e0e0e0 80px);
            background-size: 200px 100%;
            animation: skeleton-shine 1.5s infinite linear;
            border-radius: 8px;
        }

        .skeleton-cover {
            width: 80px;
            height: 80px;
            flex-shrink: 0;
            margin-right: 15px;
        }

        .skeleton-info {
            display: flex;
            flex-direction: column;
            justify-content: center;
            flex: 1;

            .skeleton-title {
                width: 90%;
                height: 20px;
                margin-bottom: 10px;
            }

            .skeleton-meta {
                width: 60%;
                height: 14px;
            }
        }
    }
}

.related-articles {
    margin: 20px 0 30px;
    padding: 24px;
    background: var(--card-bg, #fff);
    border-radius: 12px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;

    &::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 4px;
        background: linear-gradient(90deg, var(--primary, #409EFF) 0%, rgba(64, 158, 255, 0.6) 100%);
    }

    &:hover {
        box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
        transform: translateY(-2px);
    }

    .related-title {
        font-size: 20px;
        font-weight: 600;
        margin-bottom: 20px;
        padding-bottom: 15px;
        border-bottom: 1px solid var(--border-color, #f0f0f0);
        color: var(--text-primary, #333);
        display: flex;
        align-items: center;
        transition: all 0.3s ease;

        i {
            margin-right: 12px;
            color: var(--primary, #409EFF);
            font-size: 1.2em;
            background: rgba(64, 158, 255, 0.1);
            width: 32px;
            height: 32px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
            box-shadow: 0 2px 6px rgba(64, 158, 255, 0.15);
        }

        &:hover i {
            transform: rotate(30deg);
            box-shadow: 0 4px 10px rgba(64, 158, 255, 0.25);
        }
    }

    .related-list {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
        gap: 20px;

        .related-item {
            display: flex;
            background: var(--hover-bg, #f9f9f9);
            border-radius: 12px;
            overflow: hidden;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
            border: 1px solid var(--border-color, #eaeaea);
            position: relative;

            &:hover {
                transform: translateY(-5px);
                box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
                border-color: var(--primary, #409EFF);

                .related-item-title {
                    color: var(--primary, #409EFF);
                }

                .cover-overlay {
                    opacity: 1;
                }

                .cover-image {
                    transform: scale(1.1);
                    filter: brightness(1.05);
                }
            }

            .related-item-cover {
                width: 100px;
                height: 100%;
                flex-shrink: 0;
                position: relative;
                overflow: hidden;

                .cover-image {
                    width: 100%;
                    height: 100%;
                    object-fit: cover;
                    transition: all 0.5s ease;
                }

                .cover-overlay {
                    position: absolute;
                    top: 0;
                    left: 0;
                    width: 100%;
                    height: 100%;
                    background: rgba(64, 158, 255, 0.7);
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    opacity: 0;
                    transition: all 0.3s ease;

                    i {
                        color: white;
                        font-size: 1.5rem;
                        transform: scale(0.8);
                        transition: transform 0.3s ease;
                        filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
                    }

                    &:hover i {
                        transform: scale(1.2);
                    }
                }
            }

            .related-item-info {
                padding: 15px;
                display: flex;
                flex-direction: column;
                flex: 1;
                overflow: hidden;

                .related-item-title {
                    font-size: 16px;
                    font-weight: 500;
                    color: var(--text-primary, #333);
                    margin-bottom: 10px;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                    line-height: 1.4;
                    transition: color 0.3s ease;
                    min-height: 44px; // 2行的高度
                    position: relative;

                    &::after {
                        content: '';
                        position: absolute;
                        bottom: -3px;
                        left: 0;
                        width: 0;
                        height: 2px;
                        background-color: var(--primary, #409EFF);
                        transition: width 0.3s ease;
                        border-radius: 1px;
                    }
                }

                .related-item-meta {
                    display: flex;
                    flex-wrap: wrap;
                    gap: 10px;
                    font-size: 13px;
                    color: var(--text-secondary, #999);
                    margin-top: auto;
                    transition: all 0.3s ease;

                    span {
                        display: inline-flex;
                        align-items: center;

                        &.category-tag {
                            background: rgba(64, 158, 255, 0.1);
                            color: var(--primary, #409EFF);
                            padding: 4px 10px;
                            border-radius: 12px;
                            font-weight: 500;
                            box-shadow: 0 1px 3px rgba(64, 158, 255, 0.15);
                            border: 1px solid rgba(64, 158, 255, 0.1);
                            transition: all 0.3s ease;

                            &:hover {
                                background: rgba(64, 158, 255, 0.15);
                                box-shadow: 0 2px 5px rgba(64, 158, 255, 0.25);
                                transform: translateY(-1px);
                            }

                            i {
                                margin-right: 4px;
                            }
                        }

                        &.read-count {
                            i {
                                margin-right: 4px;
                                color: var(--primary, #409EFF);
                            }
                        }
                    }
                }
            }

            &:hover .related-item-title::after {
                width: 50px;
            }
        }
    }

    .empty-related {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 40px 0;
        color: var(--text-secondary, #999);
        transition: all 0.3s ease;
        background: var(--hover-bg, rgba(0, 0, 0, 0.02));
        border-radius: 12px;
        margin-top: 10px;
        border: 1px dashed var(--border-color, #eaeaea);

        i {
            font-size: 40px;
            margin-bottom: 15px;
            opacity: 0.6;
            color: var(--primary, #409EFF);
            animation: pulse 2s infinite;
            filter: drop-shadow(0 3px 5px rgba(64, 158, 255, 0.2));
        }

        p {
            margin: 0;
            font-size: 16px;
            font-weight: 500;
        }
    }

    @media (max-width: 768px) {
        padding: 20px;

        .related-list {
            grid-template-columns: 1fr;
        }

        .related-item .related-item-cover {
            width: 80px !important;
        }
    }

    @keyframes pulse {
        0% {
            transform: scale(0.95);
            opacity: 0.7;
        }

        50% {
            transform: scale(1.05);
            opacity: 0.9;
        }

        100% {
            transform: scale(0.95);
            opacity: 0.7;
        }
    }
}

/* 暗黑模式样式 */
@media (prefers-color-scheme: dark) {
    .related-list-skeleton {
        .skeleton-item {
            background: #2a2a2a;
            border-color: #333;

            .skeleton-cover,
            .skeleton-title,
            .skeleton-meta {
                background-color: #3a3a3a;
                background-image: linear-gradient(90deg,
                        #3a3a3a 0px,
                        #4a4a4a 40px,
                        #3a3a3a 80px);
            }
        }
    }

    .related-articles {
        background: #1f1f1f;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);

        .related-title {
            border-bottom-color: #333;
            color: #e0e0e0;

            i {
                background: rgba(64, 158, 255, 0.15);
                box-shadow: 0 2px 6px rgba(64, 158, 255, 0.2);
            }
        }

        .related-list {
            .related-item {
                background: #2a2a2a;
                box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
                border: 1px solid #333;

                &:hover {
                    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
                    border-color: #409EFF;
                }

                .related-item-info {
                    .related-item-title {
                        color: #e0e0e0;
                    }

                    .related-item-meta {
                        .category-tag {
                            background: rgba(64, 158, 255, 0.15);
                            border-color: rgba(64, 158, 255, 0.15);
                            box-shadow: 0 1px 3px rgba(64, 158, 255, 0.2);
                        }
                    }
                }
            }
        }

        .empty-related {
            color: #888;
            background: rgba(255, 255, 255, 0.05);
            border-color: #333;
        }
    }
}

/* 兼容自定义暗黑模式类 */
.dark-mode .related-articles,
html.dark .related-articles,
body.dark .related-articles {
    background: #1f1f1f;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);

    .related-title {
        border-bottom-color: #333;
        color: #e0e0e0;

        i {
            background: rgba(64, 158, 255, 0.15);
            box-shadow: 0 2px 6px rgba(64, 158, 255, 0.2);
        }
    }

    .related-list {
        .related-item {
            background: #2a2a2a;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
            border: 1px solid #333;

            &:hover {
                box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
                border-color: #409EFF;
            }

            .related-item-info {
                .related-item-title {
                    color: #e0e0e0;
                }

                .related-item-meta {
                    .category-tag {
                        background: rgba(64, 158, 255, 0.15);
                        border-color: rgba(64, 158, 255, 0.15);
                        box-shadow: 0 1px 3px rgba(64, 158, 255, 0.2);
                    }
                }
            }
        }
    }

    .empty-related {
        color: #888;
        background: rgba(255, 255, 255, 0.05);
        border-color: #333;
    }
}

/* 暗黑模式骨架屏 */
.dark-mode .related-list-skeleton,
html.dark .related-list-skeleton,
body.dark .related-list-skeleton {
    .skeleton-item {
        background: #2a2a2a;
        border-color: #333;

        .skeleton-cover,
        .skeleton-title,
        .skeleton-meta {
            background-color: #3a3a3a;
            background-image: linear-gradient(90deg,
                    #3a3a3a 0px,
                    #4a4a4a 40px,
                    #3a3a3a 80px);
        }
    }
}
</style>